﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using fcwms.Common;

public partial class admin_user_role_edit : SessionManage
{
    private string action = ActionEnum.Add.ToString(); //操作类型
    private int id = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        string _action = DTRequest.GetQueryString("action");
        if (!string.IsNullOrEmpty(_action) && _action == ActionEnum.Edit.ToString())
        {
            this.action = ActionEnum.Edit.ToString();//修改类型
            if (!int.TryParse(Request.QueryString["id"] as string, out this.id))
            {
                MessageBox.Jscript(this, "传输参数不正确！", "back", "Error");
                return;
            }
            if (!new fcwms.BLL.dt_role().Exists(this.id))
            {
                MessageBox.Jscript(this, "角色不存在或已被删除！", "back", "Error");
                return;
            }
        }
        if (!IsPostBack)
        {
            rptList1.DataSource = GetNavList();
            rptList1.DataBind();
            rptList2.DataSource = GetUserList();
            rptList2.DataBind();
            rptList3.DataSource = GetInOut();
            rptList3.DataBind();
            rptList4.DataSource = GetInStorage();
            rptList4.DataBind();
            rptList5.DataSource = GetStaForm();
            rptList5.DataBind();
            RoleBind();
            if (action == ActionEnum.Edit.ToString()) //修改
            {
                ShowInfo(this.id);
            }
        }
    }

    #region 角色类型==============================
    private void RoleBind()
    {
        //StringBuilder strSql = new StringBuilder();
        //strSql.Append("select role_type from dt_role  ");
        //DataTable dt = DbHelperSQL.Query(strSql.ToString()).Tables[0];
        //int role_type = int.Parse(dt.Rows[0]["role_type"].ToString());

        fcwms.Model.dt_users model = GetAdminInfo();

        ddlRoleType.Items.Clear();
        ddlRoleType.Items.Add(new ListItem("请选择类型...", ""));
        if (model.role_type < 2)
        {
            ddlRoleType.Items.Add(new ListItem("超级用户", "1"));
        }
        ddlRoleType.Items.Add(new ListItem("系统用户", "2"));
    }
    #endregion

    #region 赋值操作==============================
    public void ShowInfo(int _id)
    {
        fcwms.BLL.dt_role bll = new fcwms.BLL.dt_role();
        fcwms.Model.dt_role model = bll.GetModel(_id);
        txtRoleName.Text = model.role_name;
        //txtRoleName.ReadOnly = true;
        //txtRoleName.Enabled = false;
        hidRoleName.Value = model.role_name;
        //ddlRoleType.SelectedValue = model.role_type.ToString();
        this.ddlRoleType.Value = model.role_type.ToString();
        //菜单设置
        for (int i = 0; i < rptList1.Items.Count; i++)
        {
            string navName = ((HiddenField)rptList1.Items[i].FindControl("hidName")).Value;
            string navValue = ((HtmlInputCheckBox)rptList1.Items[i].FindControl("cblNavName")).Value;
            if (model.dt_role_values != null)
            {
                fcwms.Model.dt_role_value modelt = model.dt_role_values.Find(p => p.menu_name == navName && p.action_type == navValue);
                if (modelt != null)
                {
                    ((HiddenField)rptList1.Items[i].FindControl("hidId")).Value = modelt.id.ToString();
                    ((HtmlInputCheckBox)rptList1.Items[i].FindControl("cblNavName")).Checked = true;
                }
            }
        }

        //用户设置
        for (int i = 0; i < rptList2.Items.Count; i++)
        {
            string navName = ((HiddenField)rptList2.Items[i].FindControl("hidName")).Value;
            string navValue = ((HtmlInputCheckBox)rptList2.Items[i].FindControl("cblNavName")).Value;
            if (model.dt_role_values != null)
            {
                fcwms.Model.dt_role_value modelt = model.dt_role_values.Find(p => p.menu_name == navName && p.action_type == navValue);
                if (modelt != null)
                {
                    ((HiddenField)rptList2.Items[i].FindControl("hidId")).Value = modelt.id.ToString();
                    ((HtmlInputCheckBox)rptList2.Items[i].FindControl("cblNavName")).Checked = true;
                }
            }
        }
    }
    #endregion

    #region 系统菜单==============================
    /// <summary>
    /// 系统设置菜单
    /// </summary>
    private DataTable GetNavList()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("name", Type.GetType("System.String"));
        dt.Columns.Add("value", Type.GetType("System.String"));
        dt.Columns.Add("text", Type.GetType("System.String"));

        dt.Rows.Add("crku_msg", ActionEnum.View.ToString(), "入库出库");
        dt.Rows.Add("sys_msg", ActionEnum.View.ToString(), "库内管理");
        dt.Rows.Add("play_msg", ActionEnum.View.ToString(), "挑拨管理");
        dt.Rows.Add("form_msg", ActionEnum.View.ToString(), "统计报表");
        dt.Rows.Add("sys_msg", ActionEnum.View.ToString(), "系统管理");

        return dt;
    }
    #endregion

    #region 入库出库==============================
    /// <summary>
    /// 入库出库设置菜单
    /// </summary>
    private DataTable GetInOut()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("name", Type.GetType("System.String"));
        dt.Columns.Add("value", Type.GetType("System.String"));
        dt.Columns.Add("text", Type.GetType("System.String"));

        dt.Rows.Add("sys_inreg", ActionEnum.View.ToString(), "查看入库登记");
        dt.Rows.Add("sys_inreg", ActionEnum.Add.ToString(), "添加入库登记");
        dt.Rows.Add("sys_inreg", ActionEnum.Edit.ToString(), "修改入库登记");
        dt.Rows.Add("sys_inreg", ActionEnum.Delete.ToString(), "删除入库登记");

        dt.Rows.Add("sys_cost", ActionEnum.View.ToString(), "查看费用结算");
        dt.Rows.Add("sys_cost", ActionEnum.Add.ToString(), "添加费用结算");
        dt.Rows.Add("sys_cost", ActionEnum.Edit.ToString(), "修改费用结算");
        dt.Rows.Add("sys_cost", ActionEnum.Delete.ToString(), "删除费用结算");

        dt.Rows.Add("sys_outreg", ActionEnum.View.ToString(), "查看出库登记");
        dt.Rows.Add("sys_outreg", ActionEnum.Add.ToString(), "添加出库登记");
        dt.Rows.Add("sys_outreg", ActionEnum.Edit.ToString(), "修改出库登记");
        dt.Rows.Add("sys_outreg", ActionEnum.Delete.ToString(), "删除出库登记");

        return dt;
    }
    #endregion

    #region 库内管理==============================
    /// <summary>
    /// 库内设置菜单
    /// </summary>
    private DataTable GetInStorage()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("name", Type.GetType("System.String"));
        dt.Columns.Add("value", Type.GetType("System.String"));
        dt.Columns.Add("text", Type.GetType("System.String"));

        dt.Rows.Add("sys_check", ActionEnum.View.ToString(), "查看盘点");
        dt.Rows.Add("sys_check", ActionEnum.Add.ToString(), "添加盘点");
        dt.Rows.Add("sys_check", ActionEnum.Edit.ToString(), "修改盘点");
        dt.Rows.Add("sys_check", ActionEnum.Delete.ToString(), "删除盘点");

        return dt;
    }
    #endregion

    #region 统计报表==============================
    /// <summary>
    /// 统计报表设置菜单
    /// </summary>
    private DataTable GetStaForm()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("name", Type.GetType("System.String"));
        dt.Columns.Add("value", Type.GetType("System.String"));
        dt.Columns.Add("text", Type.GetType("System.String"));

        dt.Rows.Add("ruku_inquire", ActionEnum.View.ToString(), "查看入库查询");
        dt.Rows.Add("ruku_inquire", ActionEnum.Add.ToString(), "添加入库查询");
        dt.Rows.Add("ruku_inquire", ActionEnum.Edit.ToString(), "修改入库查询");
        dt.Rows.Add("ruku_inquire", ActionEnum.Delete.ToString(), "删除入库查询");

        return dt;
    }
    #endregion

    #region 用户菜单==============================
    /// <summary>
    /// 用户设置菜单
    /// </summary>
    private DataTable GetUserList()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("name", Type.GetType("System.String"));
        dt.Columns.Add("value", Type.GetType("System.String"));
        dt.Columns.Add("text", Type.GetType("System.String"));

        dt.Rows.Add("sys_user", ActionEnum.View.ToString(), "查看用户");
        dt.Rows.Add("sys_user", ActionEnum.Add.ToString(), "添加用户");
        dt.Rows.Add("sys_user", ActionEnum.Edit.ToString(), "修改用户");
        dt.Rows.Add("sys_user", ActionEnum.Delete.ToString(), "删除用户");

        dt.Rows.Add("sys_role", ActionEnum.View.ToString(), "查看角色");
        dt.Rows.Add("sys_role", ActionEnum.Add.ToString(), "添加角色");
        dt.Rows.Add("sys_role", ActionEnum.Edit.ToString(), "修改角色");
        dt.Rows.Add("sys_role", ActionEnum.Delete.ToString(), "删除角色");

        dt.Rows.Add("sys_branch", ActionEnum.View.ToString(), "查看部门");
        dt.Rows.Add("sys_branch", ActionEnum.Add.ToString(), "添加部门");
        dt.Rows.Add("sys_branch", ActionEnum.Edit.ToString(), "修改部门");
        dt.Rows.Add("sys_branch", ActionEnum.Delete.ToString(), "删除部门");

        dt.Rows.Add("sys_storage", ActionEnum.View.ToString(), "查看仓库");
        dt.Rows.Add("sys_storage", ActionEnum.Add.ToString(), "添加仓库");
        dt.Rows.Add("sys_storage", ActionEnum.Edit.ToString(), "修改仓库");
        dt.Rows.Add("sys_storage", ActionEnum.Delete.ToString(), "删除仓库");

        dt.Rows.Add("sys_cost", ActionEnum.View.ToString(), "查看费用");
        dt.Rows.Add("sys_cost", ActionEnum.Add.ToString(), "添加费用");
        dt.Rows.Add("sys_cost", ActionEnum.Edit.ToString(), "修改费用");
        dt.Rows.Add("sys_cost", ActionEnum.Delete.ToString(), "删除费用");

        dt.Rows.Add("sys_rukuway", ActionEnum.View.ToString(), "查看入库方式");
        dt.Rows.Add("sys_rukuway", ActionEnum.Add.ToString(), "添加入库方式");
        dt.Rows.Add("sys_rukuway", ActionEnum.Edit.ToString(), "修改入库方式");
        dt.Rows.Add("sys_rukuway", ActionEnum.Delete.ToString(), "删除入库方式");

        dt.Rows.Add("sys_chukuway", ActionEnum.View.ToString(), "查看出库方式");
        dt.Rows.Add("sys_chukuway", ActionEnum.Add.ToString(), "添加出库方式");
        dt.Rows.Add("sys_chukuway", ActionEnum.Edit.ToString(), "修改出库方式");
        dt.Rows.Add("sys_chukuway", ActionEnum.Delete.ToString(), "删除出库方式");
        return dt;
    }
    #endregion

    #region 增加操作==============================
    private bool DoAdd()
    {

        bool result = true;
        fcwms.Model.dt_role model = new fcwms.Model.dt_role();
        fcwms.BLL.dt_role bll = new fcwms.BLL.dt_role();

        model.role_name = txtRoleName.Text.Trim();
        //if (this.ddlRoleType.Value != "")
        //{
        model.role_type = int.Parse(ddlRoleType.Value);
        //}
        //else
        //{
        //    MessageBox.Jscript(this, "请选择角色类型", "", "Warning");
        //}

        List<fcwms.Model.dt_role_value> ls = new List<fcwms.Model.dt_role_value>();
        //菜单设置
        for (int i = 0; i < rptList1.Items.Count; i++)
        {
            HiddenField hidNavName = rptList1.Items[i].FindControl("hidName") as HiddenField;
            HtmlInputCheckBox hcbNavValue = rptList1.Items[i].FindControl("cblNavName") as HtmlInputCheckBox;
            if (hidNavName != null && hcbNavValue != null)
            {
                if (hcbNavValue.Checked == true)
                {
                    ls.Add(new fcwms.Model.dt_role_value { menu_id = 0, menu_name = hidNavName.Value, action_type = hcbNavValue.Value });
                }
            }
        }
        //用户设置
        for (int i = 0; i < rptList2.Items.Count; i++)
        {
            HiddenField hidNavName = rptList2.Items[i].FindControl("hidName") as HiddenField;
            HtmlInputCheckBox hcbNavValue = rptList2.Items[i].FindControl("cblNavName") as HtmlInputCheckBox;
            if (hidNavName != null && hcbNavValue != null)
            {
                if (hcbNavValue.Checked == true)
                {
                    ls.Add(new fcwms.Model.dt_role_value { menu_id = 0, menu_name = hidNavName.Value, action_type = hcbNavValue.Value });
                }
            }
        }
        model.dt_role_values = ls;
        if (bll.Add(model) < 1)
        {
            result = false;
        }
        return result;
    }
    #endregion

    #region 修改操作==============================
    private bool DoEdit(int _id)
    {
        bool result = true;
        fcwms.BLL.dt_role bll = new fcwms.BLL.dt_role();
        fcwms.Model.dt_role model = bll.GetModel(_id);

        model.role_name = txtRoleName.Text.Trim();
        //if (this.ddlRoleType.Value != "")
        //{
        model.role_type = int.Parse(ddlRoleType.Value);
        //}
        //else
        //{
        //    MessageBox.Jscript(this, "请选择角色类型", "", "Warning");
        //}


        List<fcwms.Model.dt_role_value> ls = new List<fcwms.Model.dt_role_value>();
        //菜单设置
        for (int i = 0; i < rptList1.Items.Count; i++)
        {
            int hidId = Convert.ToInt32(((HiddenField)rptList1.Items[i].FindControl("hidId")).Value);
            HiddenField hidNavName = rptList1.Items[i].FindControl("hidName") as HiddenField;
            HtmlInputCheckBox hcbNavValue = rptList1.Items[i].FindControl("cblNavName") as HtmlInputCheckBox;
            if (hidNavName != null && hcbNavValue != null)
            {
                if (hcbNavValue.Checked == true)
                {
                    ls.Add(new fcwms.Model.dt_role_value { id = hidId, role_id = _id, menu_id = 0, menu_name = hidNavName.Value, action_type = hcbNavValue.Value });
                }
            }
        }
        //用户设置
        for (int i = 0; i < rptList2.Items.Count; i++)
        {
            int hidId = Convert.ToInt32(((HiddenField)rptList2.Items[i].FindControl("hidId")).Value);
            HiddenField hidNavName = rptList2.Items[i].FindControl("hidName") as HiddenField;
            HtmlInputCheckBox hcbNavValue = rptList2.Items[i].FindControl("cblNavName") as HtmlInputCheckBox;
            if (hidNavName != null && hcbNavValue != null)
            {
                if (hcbNavValue.Checked == true)
                {
                    ls.Add(new fcwms.Model.dt_role_value { id = hidId, role_id = _id, menu_id = 0, menu_name = hidNavName.Value, action_type = hcbNavValue.Value });
                }
            }
        }
        string role_name = bll.GetTitle(_id);
        if (role_name == "超级管理员")
        {
            MessageBox.Jscript(this, "不能修改该角色！", "role_list.aspx", "Error");
        }
        //else if (new fcwms.BLL.dt_role().GetTitle(_id) != model.role_name && bll.Exists(model.role_name))
        //{
        //    MessageBox.Jscript(this, "此角色名已存在，请换一个", "", "Warning");
        //}
        else
        {
            model.dt_role_values = ls;
            if (!bll.Update(model))
            {
                result = false;
            }
        }
        return result;
    }
    #endregion

    #region 保存操作==============================
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (action == ActionEnum.Edit.ToString())
        {
            ChkAdminLevel("sys_role", ActionEnum.Edit.ToString()); //检查权限
            if (!DoEdit(this.id))
            {
                MessageBox.Jscript(this, "保存过程中发生错误啦！", "", "Error");
                return;
            }
            MessageBox.Jscript(this, "修改角色成功啦！", "role_list.aspx", "Success");
        }
        else
        {
            ChkAdminLevel("sys_role", ActionEnum.Add.ToString()); //检查权限
            if (!DoAdd())
            {
                MessageBox.Jscript(this, "保存过程中发生错误啦！", "", "Error");
                return;
            }
            MessageBox.Jscript(this, "添加角色成功啦！", "role_list.aspx", "Success");
        }
    }
    #endregion
}
